package com.example.mine_platform.service.impl;

import com.example.mine_platform.mapper.SchedulingMapper;
import com.example.mine_platform.mapper.UserMapper;
import com.example.mine_platform.pojo.dto.SchedulingLists;
import com.example.mine_platform.service.SchedulingService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
public class SchedulingServiceImpl implements SchedulingService {
    @Resource
    private SchedulingMapper schedulingMapper;
    @Resource
    private UserMapper userMapper;
    LocalDate date;
    LocalDate thisWeekMondayDate;
    LocalDate thisWeekFridayDate ;
    LocalDate nextWeekFridayDate ;
    LocalDate nextWeekMondayDate ;
    DateTimeFormatter formatter ;

    @Override
    public int[][] getOne(int userId, int index){
        int[][] now=new int[9][5];
        int[][] next=new int[9][5];
        List<String> list =schedulingMapper.getOne(userId);
        getBoundary();
        for (String i:list) {
            LocalDateTime dateTime = LocalDateTime.parse(i, formatter);
            LocalDate date1 = dateTime.toLocalDate();
            int hour=dateTime.getHour();
            if(hour>=9&&hour<=11) hour-=9;
            else if(hour>=14&&hour<=17) hour-=11;
            else hour-=12;
            if (!date1.isBefore(thisWeekMondayDate)&&!date1.isAfter(thisWeekFridayDate))
                now[hour][date1.getDayOfWeek().getValue()-1]=1;
            if (!date1.isBefore(nextWeekMondayDate)&&!date1.isAfter(nextWeekFridayDate))
                next[hour][date1.getDayOfWeek().getValue()-1]=1;
        }
        if(index==1) return now;
        else  return next;
    }
    @Override
    public SchedulingLists getAll(int index){
        int sum= userMapper.count();
        int[][] now=new int[9][5];
        int[][] next=new int[9][5];
        List<String> list =schedulingMapper.getAll();
        getBoundary();
        for (String i:list) {
            LocalDateTime dateTime = LocalDateTime.parse(i, formatter);
            LocalDate date1 = dateTime.toLocalDate();
            int hour=dateTime.getHour();
            if(hour>=9&&hour<=11) hour-=9;
            else if(hour>=14&&hour<=17) hour-=11;
            else hour-=12;
            if (!date1.isBefore(thisWeekMondayDate)&&!date1.isAfter(thisWeekFridayDate))
                now[hour][date1.getDayOfWeek().getValue()-1]+=1;
            if (!date1.isBefore(nextWeekMondayDate)&&!date1.isAfter(nextWeekFridayDate))
                next[hour][date1.getDayOfWeek().getValue()-1]+=1;
        }
        SchedulingLists schedulingLists1=new SchedulingLists(sum,now);
        SchedulingLists schedulingLists2=new SchedulingLists(sum,next);
        if(index==1) return schedulingLists1;
        else  return schedulingLists2;
    }

    @Override
    public void changeTime(int userId,int index,int day,int time,int status){
        LocalDate date=LocalDate.now().with(DayOfWeek.MONDAY);
        LocalDate nowdate,nextdate;
        LocalDateTime dateTime;
        if(index==1) {
            nowdate = date.plusDays(day - 1);
            dateTime=nowdate.atStartOfDay();
        } else{
            nextdate=date.plusDays(7+day-1);
            dateTime=nextdate.atStartOfDay();
        }
        int period=0;
        switch (time){
            case 1:period=9;break;
            case 2:period=10;break;
            case 3:period=11;break;
            case 4:period=14;break;
            case 5:period=15;break;
            case 6:period=16;break;
            case 7:period=17;break;
            case 8:period=19;break;
            case 9:period=20;break;
        }
        LocalDateTime Scheduling = dateTime.withHour(period).withMinute(0).withSecond(0);
        if(status==1) {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime update = LocalDateTime.now();
            schedulingMapper.add(userId, Scheduling, now, update, 0);
        }
        else
            schedulingMapper.updatewithdeleted(userId,Scheduling);
    }
    public void getBoundary(){
        date = LocalDate.now().with(DayOfWeek.MONDAY);
        thisWeekMondayDate = date;
        thisWeekFridayDate = date.plusDays(4);
        nextWeekFridayDate = date.plusDays(11);
        nextWeekMondayDate = date.plusDays(7);
        formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    }
}
